package array;

import java.util.PriorityQueue;

public class MaximizeSumOfArrayAfterKNegations1005 {
    public int largestSumAfterKNegations(int[] nums, int k) {
        PriorityQueue<Integer> priorityQueue = new PriorityQueue<>();
        for (int i = 0; i < nums.length; i++) {
            priorityQueue.offer(nums[i]);
        }

        for (int i = 0; i < k; i++) {
            Integer n = priorityQueue.poll();
            n=-n;
            priorityQueue.offer(n);
        }

        int sum = 0;
        for (int i = 0; i < nums.length; i++) {
            sum+= priorityQueue.poll();
        }
        return sum;
    }
}
